#!/usr/bin/env python3
import sys
from scapy.all import Ether, Packet, bind_layers, get_if_list, sniff
from scapy.fields import BitField
from scapy.layers.inet import IP

def get_if():
    for i in get_if_list():
        if "eth0" in i:
            return i
    print("Cannot find eth0 interface")
    sys.exit(1)

class SourceRoute(Packet):
   fields_desc = [ BitField("bos", 0, 1),
                   BitField("port", 0, 15)]
bind_layers(Ether, SourceRoute, type=0x1234)
bind_layers(SourceRoute, SourceRoute, bos=0)

def handle_pkt(pkt):
    print("=== got a packet ===")
    if pkt.haslayer(SourceRoute):
        print("[SR] hops present")
    else:
        print("[IP] plain IPv4")
    pkt.show2()
    sys.stdout.flush()

def main():
    iface = get_if() or 'eth0'
    print("sniffing on %s" % iface); sys.stdout.flush()
    sniff(filter="udp and port 4321", iface=iface, prn=lambda x: handle_pkt(x))

if __name__ == '__main__':
    main()